package com.droid.lib.controls.widget;

import com.droid.lib.controls.util.MResource;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ImageButton;

/**
 *
 * 菜单类浮动小球子控件，小球点击不可切换图片
 * @author Administrator
 *
 */
@SuppressLint("NewApi")
public class FloatingActionButton extends ImageButton {
	public static final int TYPE_NORMAL = 0;
	public static final int TYPE_MINI = 1;
	protected int type = TYPE_NORMAL;
	protected int shadowSize;
	protected boolean isMarginsSet = false;

	protected int colorPressed;
	protected int colorDisabled;
	protected int colorNormal;
	protected int normalIcon;
	protected Drawable normalIconDrawable;
	protected String labelText;
 
	public FloatingActionButton(Context context) {
		super(context);
	}
	public FloatingActionButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
	}

	public FloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context, attrs);
	}

	protected void init(Context context, AttributeSet attrs) {
		shadowSize =FloatingActionButtonSet.FloatingActionButton.fabShadowSize;
		colorNormal = FloatingActionButtonSet.FloatingActionButton.inboxFab;
		colorPressed = FloatingActionButtonSet.FloatingActionButton.inboxFabPressed;
		colorDisabled = FloatingActionButtonSet.FloatingActionButton.colorDisabled;
		if (attrs != null)
			initAttributes(context, attrs);
		updateBackground();
	}
	public void setFloatingActionButtonSetting(Context context) {
		init(context, null);
	}

	protected void initAttributes(Context context, AttributeSet attributeSet) {
		if (FloatingActionButtonSet.FloatingActionButton.defaultPressedColor || colorPressed == 0) {
			setBackgroundColorInner(colorNormal, true);
		}
		normalIcon = android.R.drawable.ic_menu_send;
		labelText = FloatingActionButtonSet.FloatingActionButton.labelText;
	}

	protected void updateBackground() {
		LayerDrawable layerDrawable = new LayerDrawable(
				new Drawable[] { createFillStateListDrawable(), getIconDrawable() });

		int circleSize = getDimension(type == TYPE_NORMAL ?FloatingActionButtonSet.FloatingActionButton.fabSizeNormal : FloatingActionButtonSet.FloatingActionButton.fabSizeMini);
		int inset = (circleSize
				- getDimension(type == TYPE_NORMAL ? FloatingActionButtonSet.FloatingActionButton.fabIconSize : FloatingActionButtonSet.FloatingActionButton.fabIconSizeMini)) / 2;

		layerDrawable.setLayerInset(1, shadowSize + inset, shadowSize + inset, shadowSize + inset, shadowSize + inset);
		setBackground(layerDrawable);
	}

	protected StateListDrawable createFillStateListDrawable() {
		StateListDrawable drawable = new StateListDrawable();
		drawable.addState(new int[] { android.R.attr.state_pressed }, createFillDrawable(colorPressed));
		drawable.addState(new int[] { -android.R.attr.state_enabled }, createFillDrawable(colorDisabled));
		drawable.addState(new int[] {}, createFillDrawable(colorNormal));
		return drawable;
	}

	protected Drawable createFillDrawable(int color) {
		OvalShape ovalShape = new OvalShape();
		ShapeDrawable shapeDrawable = new ShapeDrawable(ovalShape);
		shapeDrawable.getPaint().setColor(color);

		Drawable shadowDrawable = (type == TYPE_NORMAL ? MResource.getDrawableFromAssets(getContext(), "shadow.png")
				: MResource.getDrawableFromAssets(getContext(), "shadow_mini.png"));
		LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] { shadowDrawable, shapeDrawable });
		layerDrawable.setLayerInset(1, shadowSize, shadowSize, shadowSize, shadowSize);
		return layerDrawable;
	}

	protected Drawable getIconDrawable() {
		if (normalIconDrawable == null)
			normalIconDrawable = getCopyOfDrawableFromResources(normalIcon);
		return normalIconDrawable;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int size = getDimension(type == TYPE_NORMAL ? FloatingActionButtonSet.FloatingActionButton.fabSizeNormal :  FloatingActionButtonSet.FloatingActionButton.fabSizeMini);
		size += shadowSize * 2;
		setMarginsWithoutShadow();
		setMeasuredDimension(size, size);
	}

	protected void setMarginsWithoutShadow() {
		if (!isMarginsSet) {
			if (getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
				ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) getLayoutParams();
				int leftMargin = layoutParams.leftMargin - shadowSize;
				int topMargin = layoutParams.topMargin - shadowSize;
				int rightMargin = layoutParams.rightMargin - shadowSize;
				int bottomMargin = layoutParams.bottomMargin - shadowSize;
				layoutParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);

				requestLayout();
				isMarginsSet = true;
			}
		}
	}

	protected int getDimension(int id) {
		return id;
	}

	protected int getColor(int id) {
		return id;
	}

	protected TypedArray getTypedArray(Context context, AttributeSet attributeSet, int[] attr) {
		return context.obtainStyledAttributes(attributeSet, attr, 0, 0);
	}

	public String getLabelText() {
		return labelText;
	}

	public void setLabelText(String labelText) {
		this.labelText = labelText;
	}

	protected Drawable getCopyOfDrawableFromResources(int rid) {
		if (rid != 0) {
			return getResources().getDrawable(rid).mutate().getConstantState().newDrawable();
		} else {
			return new ColorDrawable(Color.TRANSPARENT);
		}
	}

	@Override
	public void setAlpha(float alpha) {
		super.setAlpha(alpha);
		if (alpha == 0) {
			setVisibility(GONE);
			setClickable(false);
		} else {
			setVisibility(VISIBLE);
			setClickable(true);
		}
	}

	public void setIconDrawable(int resId,int type) {
		this.normalIcon = resId;
		this.normalIconDrawable = null;
		this.type=type;
		updateBackground();
	}

	public void setBackgroundColor(int normalColor, int pressedColor) {
		this.colorNormal = normalColor;
		this.colorPressed = pressedColor;
		updateBackground();
	}

	private void setBackgroundColorInner(int normalColor, Boolean calcPressedColorBrighter) {
		this.colorNormal = normalColor;

		if (calcPressedColorBrighter == null)
			this.colorPressed = normalColor;
		else {
			float[] hsv = new float[3];
			Color.colorToHSV(normalColor, hsv);
			hsv[2] += calcPressedColorBrighter ? +0.15 : -0.15;

			this.colorPressed = Color.HSVToColor(Color.alpha(normalColor), hsv);
		}
	}

	public void setBackgroundColor(int normalColor, Boolean calcPressedColorBrighter) {
		setBackgroundColorInner(normalColor, calcPressedColorBrighter);
		updateBackground();
	}
}